package com.chzc.fm.duotin.ui.channel;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chzc.fm.duotin.R;
import com.chzc.fm.duotin.adapter.recyclerview.HotAnLinearLayoutAdapter;
import com.chzc.fm.duotin.bean.channel.AnchorSpecial;
import com.chzc.fm.duotin.utils.DuotinTask;
import com.chzc.fm.duotin.utils.GlobalParam;
import com.chzc.fm.duotin.utils.HttpUtil;
import com.chzc.fm.duotin.utils.LogUtil;
import com.chzc.fm.duotin.widget.TitleBar;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * Created by Administrator on 2016/7/5.
 */
public class NewAnchorActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private TitleBar titleBar;
    private HotAnLinearLayoutAdapter adapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_newanchor);

        initViews();
        initEvents();
        initDatas();
    }

    public void initViews()
    {
        recyclerView = (RecyclerView) findViewById(R.id.newanchor_content_rv);
        titleBar = (TitleBar) findViewById(R.id.newanchor_topbar_tb);

        LinearLayoutManager manager = new LinearLayoutManager(NewAnchorActivity.this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(manager);
    }

    public void initEvents()
    {

    }

    public void initDatas()
    {
        titleBar.setLeft(true,R.drawable.arrow_back_white);
        titleBar.setRight(true);
        titleBar.setSearchBarVisibilty(false);
        titleBar.setTitleBarVisibility(true,"新晋主播");
        titleBar.setLeftListener(new TitleBar.mTitleBarListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        getNewAnchor(new DuotinTask.IRequestCallback() {
            @Override
            public void success(Object object) {

                try {
                    JSONObject root = new JSONObject(object.toString());
                    String error_msg = root.getString("error_msg");
                    if("SUCCESS".equals(error_msg))
                    {
                        LogUtil.w("解析成功");
                        JSONObject data = root.getJSONObject("data");
                        JSONArray datalist = data.getJSONArray("data_list");
                        List<AnchorSpecial> specialList = AnchorSpecial.arrayAnchorSpecialFromData(datalist.toString());

                        adapter = new HotAnLinearLayoutAdapter(NewAnchorActivity.this,specialList);
                        recyclerView.setAdapter(adapter);

                        adapter.setOnItemClickListener(new HotAnLinearLayoutAdapter.mOnItemClickListener() {
                            @Override
                            public void OnItemClick(int position) {

                            }
                        });

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void error(String msg) {

            }
        });
    }

    public void getNewAnchor(DuotinTask.IRequestCallback callback)
    {
        DuotinTask.IRequest request = new DuotinTask.IRequest() {
            @Override
            public Object doRequest() {
                return HttpUtil.doGet(GlobalParam.URL_NEWANCHOR);
            }
        };
        new DuotinTask(request,callback).execute();
    }
}
